package org.jetlinks.community.mstn.handler;

import lombok.extern.slf4j.Slf4j;
import org.hswebframework.ezorm.core.param.Term;
import org.hswebframework.web.api.crud.entity.QueryParamEntity;
import org.hswebframework.web.authorization.Authentication;
import org.jetlinks.community.mstn.MstnAssetsHolder;
import org.jetlinks.community.mstn.exception.MstnAssetsUnknownException;
import org.jetlinks.community.mstn.logging.MstnAssetsRequestCtx;
import org.jetlinks.core.enums.ErrorCode;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

@Slf4j
public class MstnAssetsMonoQueryParamHandler implements MstnAssetsHandler{
    private MstnAssetsDefine ann;
    private boolean returnMono;
    private boolean returnFlux;

    public MstnAssetsMonoQueryParamHandler( MstnAssetsDefine ann, boolean returnMono, boolean returnFlux) {

        this.ann = ann;
        this.returnMono = returnMono;
        this.returnFlux = returnFlux;
    }
    @Override
    public Mono<Void> beforeHandler(MstnAssetsRequestCtx requestCtx) {
        return Authentication.currentReactive().flatMap(authentication -> {
            MstnAssetsHolder holder = MstnAssetsHolder.check(authentication, requestCtx, this.ann.getType());
            if (holder != null) {
                Term term = holder.injectQuery();
                if (term != null) {
                    requestCtx.setTerm(term);
                }
            }
            return Mono.empty();
        });
    }
}
